<?php

use \GatewayWorker\Lib\Gateway;

class Login
{
    public static function user_login($client_id,$data)
    {
        $tool=new Tool();
        Gateway::bindUid($client_id,$data->id);
        $session_data=Tool::ObjectToArray($data);
        $group_name=Service::get_group($data);
        Gateway::joinGroup($client_id,$group_name);
        Gateway::setSession($client_id,$session_data);
        $send=[
            "type"=>"login_res",
            "code"=>200,
            "data"=>"login_success"
        ];
        if($data->user_type=="user"){
            $url=Tool::$http_url."user/common/get_adviser?id=".$data->cid;

            $custom=$tool->curl_get($url,$data->token);
            $custom=json_decode($custom,256);
            $content=[
                "id"=>$custom["data"]["uid"],
                "name"=>$custom["data"]["name"],
                "header"=>$custom["data"]["header"]
            ];
            $send["content"]=$content;
        }
        $send=json_encode($send);
        Gateway::sendToClient($client_id,$send);
        echo "用户{$client_id}已登录,用户{$client_id}绑定id为{$data->id}\n";
    }
}
